package com.zhia.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhia.demo.dao.RecoverDao;
import com.zhia.demo.entity.RecoverEntity;
import com.zhia.demo.service.RecoverService;
import com.zhia.demo.service.ReviewService;
import com.zhia.demo.service.UserLikeService;
import com.zhia.demo.service.UserService;
import com.zhia.demo.utils.Utils;
import com.zhia.demo.vo.RecoverVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author DONG
 * @create 2023/11/4 16:44
 */
@Service
public class RecoverServiceImpl extends ServiceImpl<RecoverDao, RecoverEntity> implements RecoverService {
    @Autowired
    UserService userService;

    @Autowired
    ReviewService reviewService;

    @Autowired
    UserLikeService userLikeService;

    @Override
    public List<RecoverVo> getRecoverList(Integer id, Integer userId) {
        List<RecoverEntity> recoverEntities = this.list(new QueryWrapper<RecoverEntity>().eq("review_id", id).orderByDesc("create_time"));
        return recoverEntities.stream().map(recoverEntity -> {
            RecoverVo recoverVo = new RecoverVo();
            BeanUtils.copyProperties(recoverEntity, recoverVo);
            recoverVo.setReviewTime(Utils.formattingTime(recoverEntity.getCreateTime()));
            recoverVo.setRecoverNickName(userService.getById(recoverEntity.getUserId()).getNickname());
            if (recoverEntity.getRecoverId() != null) {
                recoverVo.setReviewNickName(getRecoverUsername(recoverEntity.getRecoverId()));
            }
            if (userId != null) {
                recoverVo.setUserIsLike(userLikeService.getUserIsLikeReview(userId, "h" + recoverEntity.getId()));
            }
            return recoverVo;
        }).collect(Collectors.toList());
    }

    private String getRecoverUsername(Integer recoverId) {

        try {
            RecoverEntity byId = this.getById(recoverId);
            return userService.getById(byId.getUserId()).getNickname();
        } catch (Exception e) {
            return "已删除评论";
        }
    }

    @Override
    public void deleteByReviewId(Integer reviewId) {
        this.baseMapper.delete(new QueryWrapper<RecoverEntity>().eq("review_id", reviewId));
    }

    @Override
    public void addRecover(RecoverEntity recoverEntity) throws Exception {
        try {
            if (reviewService.getById(recoverEntity.getReviewId()) == null) {
                throw new Exception("评论不存在");
            }
        } catch (Exception e) {
            throw new Exception("评论不存在");
        }
        recoverEntity.setGiveLike(0L);
        recoverEntity.setCreateTime(new Timestamp(new Date().getTime()));
        save(recoverEntity);
    }

    @Override
    public void likeAdd(Integer id) {
        this.baseMapper.likeAdd(id);
    }

    @Override
    public void likeDec(Integer id) {
        this.baseMapper.likeDec(id);
    }
}
